home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.1 KB  |  140 lines

  1. /*
  2.  * Display the welcome screen and find the Pcomm support files.  Returns a
  3.  * pointer to a static area (or shared memory) containing the STATUS
  4.  * structure.  All errors are fatal.
  5.  */
  6.  
  7. #define TMP_FILE    "/tmp/pcommXXXXXX"
  8.  
  9. #include <stdio.h>
  10. #include <curses.h>
  11. #include "config.h"
  12. #include "misc.h"
  13. #include "status.h"
  14.  
  15. #ifdef SHAREDMEM
  16. #include <sys/types.h>
  17. #include <sys/ipc.h>
  18. #include <sys/shm.h>
  19. #endif /* SHAREDMEM */
  20.  
  21. struct STATUS *
  22. init(short_cut)
  23. char *short_cut;
  24. {
  25.     char *strcpy();
  26.     struct STATUS *s_ptr;
  27.     void info();
  28. #ifdef SHAREDMEM
  29.     int i, mode;
  30.     extern int shm_id;
  31.     char *memset();
  32.     char *shmat();            /* comment out, if required */
  33.     void perror(), exit();
  34.  
  35.     /*
  36.      * Since the "pcomm_input" program does not run set-user/group-id
  37.      * the mode must be set so the effective ID can read/write to the
  38.      * shared memory segment.  Kinda strange... real ID's aren't used.
  39.      */
  40. #ifdef SETUGID
  41.     mode = 0666;
  42. #else /* SETUGID */
  43.     mode = 0600;
  44. #endif /* SETUGID */
  45.                     /* create a shared memory segment */
  46.     shm_id = shmget(IPC_PRIVATE, sizeof (struct STATUS),  mode|IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
  47.     if (shm_id < 0) {
  48.         endwin();
  49.         perror("shmget");
  50.         exit(1);
  51.     }
  52.     s_ptr = (struct STATUS *) shmat(shm_id, (char *) 0, 0);
  53.     if ((int) s_ptr == -1) {
  54.         endwin();
  55.         perror("shmat");
  56.         exit(1);
  57.     }
  58. #else /* SHAREDMEM */
  59.     char *mktemp(), tempfile[sizeof(TMP_FILE)];
  60.     static struct STATUS s;
  61.     s_ptr = &s;
  62. #endif /* SHAREDMEM */
  63.                     /* some defaults */
  64.     s_ptr->fd = -1;
  65.     s_ptr->dup_fd = -1;
  66.     s_ptr->add_lf = 0;
  67.     s_ptr->log = 0;
  68.     s_ptr->print = 0;
  69.     strcpy(s_ptr->log_path, "NOT_DEFINED");
  70.  
  71. #ifdef SHAREDMEM
  72.     s_ptr->clr = 0;
  73.     s_ptr->row = 0;
  74.     s_ptr->col = 0;
  75.     for (i=0; i<MAX_ROW; i++)
  76.         memset(s_ptr->vs[i], '\0', MAX_COL);
  77. #else /* SHAREDMEM */
  78.     strcpy(tempfile, TMP_FILE);
  79.     strcpy(s_ptr->vs_path, mktemp(tempfile));
  80. #endif /* SHAREDMEM */
  81.                     /* display herald if no short-cut */
  82.     if (short_cut == NULL)
  83.         info(AUTO_CLEAR);
  84.  
  85.     erase();
  86.     refresh();
  87.     return(s_ptr);
  88. }
  89.  
  90. /*
  91.  * Search the extra directory (supplied on the command line), then the
  92.  * directory in the PCOMM environmental variable, then the current working
  93.  * directory, and lastly, the default directory.
  94.  */
  95.  
  96. char *
  97. findfile(extra, name)
  98. char *extra, *name;
  99. {
  100.     int i;
  101.     char *pcomm, *getenv(), *path, pbuf[200], *getcwd(), *str_dup();
  102.     char temp[200];
  103.  
  104.                     /* see if PCOMM variable is set */
  105.     pcomm = getenv("PCOMM");
  106.     if (pcomm == NULL || *pcomm == '\0')
  107.         pcomm = NULL;
  108.     else {
  109.                     /* zap the trailing separator */
  110.         if (pcomm[strlen(pcomm)-1] == '/')
  111.             pcomm[strlen(pcomm)-1] = '\0';
  112.     }
  113.  
  114.     for (i=0; i<4; i++) {
  115.                     /* directory search order */
  116.         switch (i) {
  117.             case 0:        /* extra directory from command line */
  118.                 path = extra;
  119.                 break;
  120.             case 1:        /* PCOMM environmental variable */
  121.                 path = pcomm;
  122.                 break;
  123.             case 2:        /* current working directory */
  124.                 path = getcwd(pbuf, 200);
  125.                 break;
  126.             case 3:        /* Pcomm's default directory */
  127.                 path = DEFAULT_DIR;
  128.                 break;
  129.         }
  130.         if (path == NULL)
  131.             continue;
  132.  
  133.         sprintf(temp, "%s/%s", path, name);
  134.                     /* read permission checked */
  135.         if (!access(temp, 4))
  136.             return(str_dup(temp));
  137.     }
  138.     return(NULL);
  139. }
  140.